﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GUI.TimKiem
{
    public partial class frmHangHoa : Form
    {
        #region Khởi tạo
        Entities.HangHoa[] hienthi;
        Entities.HienThiHangHoaListview[] hienthi2;
        string hanhdong;
        bool ask = true;
        int i = 0;

        public frmHangHoa()
        {
            InitializeComponent();
            btnChon.Click += new EventHandler(btnChon_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
            dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
            dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown);
            selectData();

        }

        public frmHangHoa(string hanhdong, string name, string ss)
        {
            this.hanhdong = hanhdong;
            if (hanhdong == "TimKiem" && name == "HangHoa")
            {
                InitializeComponent();
                btnChon.Click += new EventHandler(btnChon_Click);
                btnThoat.Click += new EventHandler(btnThoat_Click);
                dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
                dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown);
                selectData(hanhdong, name);
            }

        }

        public frmHangHoa(Entities.CapNhatGiaHangHoa a)
        {
            InitializeComponent();
            btnChon.Click += new EventHandler(btnChon_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
            dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
            dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown);
            selectData(a);
        }

        public frmHangHoa(string hanhdong, string str)
        {
            this.hanhdong = hanhdong;
            if (hanhdong == "TimKiem" && str == "KH")
            {
                InitializeComponent();
                btnChon.Click += new EventHandler(btnChon_Click);
                btnThoat.Click += new EventHandler(btnThoat_Click);
                dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick2);
                dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown);
                HienThi();
            }
        }

        public frmHangHoa(string hanhdong, int number)
        {
            this.hanhdong = hanhdong;
            if (hanhdong == "CSGHH" && number == 23)
            {
                InitializeComponent();
                btnChon.Click += new EventHandler(btnChon_Click);
                btnThoat.Click += new EventHandler(btnThoat_Click);
                dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick2);
                dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown);
                HienThiCSGHH();
            }
        }

        public frmHangHoa(string makho)
        {
            InitializeComponent();
            btnChon.Click += new EventHandler(btnChon_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
            dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
            dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown);
            selectData(makho);
        }

        public frmHangHoa(Entities.KhoHang khohang)
        {
            InitializeComponent();
            btnChon.Click += new EventHandler(btnChon_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
            dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
            dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown);
            selectData(khohang);
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.ReadOnly = true;
            dgvHienThi.Columns["MaHangHoa"].Visible = true;
            dgvHienThi.Columns["TenHangHoa"].Visible = true;
            dgvHienThi.Columns["TenDonViTinh"].Visible = true;

            dgvHienThi.Columns["MaHangHoa"].HeaderText = "Mã Hàng Hóa";
            dgvHienThi.Columns["TenHangHoa"].HeaderText = "Tên Hàng Hóa";
            dgvHienThi.Columns["TenDonViTinh"].HeaderText = "Đơn Vị Tính";
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHienThi.AllowUserToAddRows = false;
            dgvHienThi.AllowUserToDeleteRows = false;
            dgvHienThi.AllowUserToResizeRows = false;
            dgvHienThi.RowHeadersVisible = false;
        }
        #endregion

        #region Chọn
        void Chon3()
        {
            if (hanhdong == "TimKiem" || hanhdong == "CSGHH")
            {
                Chon23();
            }
            else
            {
                Chon();
            }
        }
        private void Chon()
        {
            if (i < 0)
                return;
            if (dgvHienThi.RowCount == 0)
                return;
            selectedItem = (Entities.HangHoa)((Entities.HangHoa[])dgvHienThi.DataSource)[i];
            ask = false;
            this.Close();
        }
        private void Chon23()
        {
            if (i < 0)
                return;
            if (dgvHienThi.RowCount == 0)
                return;
            selectedItemHH = (Entities.HienThiHangHoaListview)((Entities.HienThiHangHoaListview[])dgvHienThi.DataSource)[i];
            ask = false;
            this.Close();
        }
        #endregion

        #region thoát
        private void Thoat()
        {
            ask = true;
            this.Close();
        }
        #endregion

        #region Tìm kiếm
        void TimKiem()
        {
            if (hanhdong == "TimKiem" || hanhdong == "CSGHH")
            {
                dgvHienThi.DataSource = (Entities.HienThiHangHoaListview[])hienthi2.Where(p => p.TenHangHoa.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0).ToArray();
            }
            else
            {
                dgvHienThi.DataSource = (Entities.HangHoa[])hienthi.Where(p => p.TenHangHoa.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0).ToArray();
            }
            fix();
        }
        #endregion

        #region Select
        public static Entities.HangHoa selectedItem
        {
            get;
            set;
        }

        public static Entities.HienThiHangHoaListview selectedItemHH
        {
            get;
            set;
        }

        Entities.ChiTietKhoHang[] chitietkhohang;

        public Entities.HangHoa[] hangHoa
        {
            get;
            set;
        }
        public void selectData(Entities.CapNhatGiaHangHoa a)
        {
            try
            {
                hangHoa = Common.StaticVariable.InterfaceObject.SelectHangHoa();
                //Entities.QuyDoiDonViTinh[] quydoi = UtilitiesVariable.client.SelectQuyDoiDVT();
                //if (quydoi == null)
                //    quydoi = new Entities.QuyDoiDonViTinh[0];
                ArrayList array = new ArrayList();
                foreach (Entities.HangHoa item in hangHoa)
                {
                    //bool kt = false;
                    //foreach (Entities.QuyDoiDonViTinh item1 in quydoi)
                    //{
                    //    if (item.MaHangHoa == item1.MaHangQuyDoi)
                    //    {
                    //        kt = true;
                    //        break;
                    //    }
                    //}
                    //if (!kt)
                    array.Add(item);
                }
                hangHoa = (Entities.HangHoa[])array.ToArray(typeof(Entities.HangHoa));
                dgvHienThi.DataSource = hangHoa;
                fix();
            }
            catch
            {
            }
        }
        public void selectData(string hanhdong, string name)
        {
            try
            {
                hangHoa = Common.StaticVariable.InterfaceObject.SelectHangHoa();
                ArrayList array = new ArrayList();
                foreach (Entities.HangHoa item in hangHoa)
                {
                    array.Add(item);
                }
                hangHoa = (Entities.HangHoa[])array.ToArray(typeof(Entities.HangHoa));
                dgvHienThi.DataSource = hangHoa;
                fix();
            }
            catch
            {
            }
        }
        public void selectData()
        {
            try
            {
                hangHoa = Common.StaticVariable.InterfaceObject.SelectHangHoa();
                //Entities.QuyDoiDonViTinh[] quydoi = UtilitiesVariable.client.SelectQuyDoiDVT();
                //if (quydoi == null)
                //    quydoi = new Entities.QuyDoiDonViTinh[0];
                ArrayList array = new ArrayList();
                foreach (Entities.HangHoa item in hangHoa)
                {
                    //bool kt = false;
                    //foreach (Entities.QuyDoiDonViTinh item1 in quydoi)
                    //{
                    //    if (item.MaHangHoa == item1.MaHangQuyDoi)
                    //    {
                    //        kt = true;
                    //        break;
                    //    }
                    //}
                    //if (!kt)
                    array.Add(item);
                }
                //Entities.GoiHang[] goihang = Common.StaticVariable.InterfaceObject.SelectGoiHang();
                //if (goihang == null)
                //    goihang = new Entities.GoiHang[0];
                //Entities.ChiTietGoiHang[] chitietgoihang = Common.StaticVariable.InterfaceObject.SelectChiTietGoiHang();
                //if (chitietgoihang == null)
                //    chitietgoihang = new Entities.ChiTietGoiHang[0];
                //Entities.HangHoa[] hh = Common.Utilities.CheckGoiHang((Entities.HangHoa[])array.ToArray(typeof(Entities.HangHoa)), goihang, chitietgoihang);
                //foreach (Entities.HangHoa item in hh)
                //{
                //    array.Add(item);
                //}
                hangHoa = (Entities.HangHoa[])array.ToArray(typeof(Entities.HangHoa));
                dgvHienThi.DataSource = hangHoa;
                fix();
                hienthi = hangHoa;
            }
            catch
            {
            }
        }

        public Entities.HangHoa[] selectData(string makho)
        {
            try
            {
                hangHoa = Common.StaticVariable.InterfaceObject.SelectHangHoa();
                Entities.KhoHang temp = new Entities.KhoHang();
                Entities.ChiTietKhoHang ch = new Entities.ChiTietKhoHang();
                ch.HanhDong = "ChiTietHangTheoKho";
                ch.MaKho = makho;
                chitietkhohang = Common.StaticVariable.InterfaceObject.Select_ChiTietKho(ch);
                ArrayList array = new ArrayList();

                foreach (Entities.HangHoa item in hangHoa)
                {
                    foreach (Entities.ChiTietKhoHang item1 in chitietkhohang)
                    {
                        if (item.MaHangHoa.ToUpper() == item1.MaHangHoa.ToUpper())
                            array.Add(item);
                    }
                }
                hangHoa = (Entities.HangHoa[])array.ToArray(typeof(Entities.HangHoa));
                for (int i = 0; i < hangHoa.Length; i++)
                {
                    for (int j = 0; j < chitietkhohang.Length; j++)
                    {
                        if (hangHoa[i].MaHangHoa == chitietkhohang[j].MaHangHoa)
                        {
                            hangHoa[i].SoLuong = Convert.ToInt32(chitietkhohang[j].SoLuong);
                            break;
                        }
                    }
                }
                dgvHienThi.DataSource = hangHoa;
                fix();
                hienthi = hangHoa;
                return hangHoa;
            }
            catch
            {
                return new Entities.HangHoa[0];
            }
        }

        public Entities.HangHoa[] selectData(Entities.KhoHang khohang)
        {
            try
            {

                hangHoa = Common.StaticVariable.InterfaceObject.SelectHangHoa();
                Entities.GoiHang[] goihang = Common.StaticVariable.InterfaceObject.SelectGoiHang(new Entities.GoiHang("1"));
                Entities.ChiTietGoiHang[] chitietgoihang = Common.StaticVariable.InterfaceObject.SelectChiTietGoiHang(new Entities.ChiTietGoiHang("1"));
                Entities.KhoHang temp = new Entities.KhoHang();
                Entities.ChiTietKhoHang ch = new Entities.ChiTietKhoHang();
                ch.HanhDong = "ChiTietHangTheoKho";
                ch.MaKho = khohang.MaKho;
                chitietkhohang = Common.StaticVariable.InterfaceObject.Select_ChiTietKho(ch);
                ArrayList array = new ArrayList();
                foreach (Entities.HangHoa item in hangHoa)
                {
                    foreach (Entities.ChiTietKhoHang item1 in chitietkhohang)
                    {
                        if (item.MaHangHoa == item1.MaHangHoa)
                            array.Add(item);
                    }
                }
                hangHoa = (Entities.HangHoa[])array.ToArray(typeof(Entities.HangHoa));
                Entities.HangHoa[] hh1 = (Entities.HangHoa[])array.ToArray(typeof(Entities.HangHoa));
                Entities.HangHoa[] hh2 = Common.Utilities.CheckGoiHang(hh1, goihang, chitietgoihang);
                foreach (Entities.HangHoa item in hh2)
                {
                    array.Add(item);
                }
                dgvHienThi.DataSource = (Entities.HangHoa[])array.ToArray(typeof(Entities.HangHoa));
                fix();
                hienthi = hangHoa;
                return hangHoa;
            }
            catch
            {
                return new Entities.HangHoa[0];
            }
        }

        /// <summary>
        /// loivt
        /// </summary>
        public Entities.HienThiHangHoaListview[] HienThi()
        {
            //Entities.CaiDatKho kho = Common.SetupWarehouse.ViewFile();
            //Entities.DonDatHangKH don = UtilitiesVariable.client.SelectHangHoaKH(kho.MaKho);


            //int count = don.GoiHang.Length + don.HangHoa.Length;
            //Entities.HienThiHangHoaListview[] hanghoa = new Entities.HienThiHangHoaListview[count];
            //ArrayList arr = new ArrayList();
            //Entities.HienThiHangHoaListview row;
            //for (int i = 0; i < don.HangHoa.Length; i++)
            //{
            //    row = new Entities.HienThiHangHoaListview();
            //    row.MaHangHoa = don.HangHoa[i].MaHangHoa;
            //    row.TenHangHoa = don.HangHoa[i].TenHangHoa;
            //    row.PhanTramChietKhau = don.HangHoa[i].Phantrankhuyenmai;
            //    row.GiaBanBuon = don.HangHoa[i].GiaBanBuon;
            //    row.GiaNhap = don.HangHoa[i].GiaNhap;
            //    row.GiaBanLe = don.HangHoa[i].GiaBanLe;
            //    row.Thue = don.HangHoa[i].GiaTriThue;
            //    arr.Add(row);
            //}
            //for (int i = 0; i < don.GoiHang.Length; i++)
            //{
            //    row = new Entities.HienThiHangHoaListview();
            //    row.MaHangHoa = don.GoiHang[i].MaGoiHang;
            //    row.TenHangHoa = don.GoiHang[i].TenGoiHang;
            //    row.GiaBanBuon = don.GoiHang[i].GiaBanBuon;
            //    row.GiaNhap = don.GoiHang[i].GiaNhap;
            //    arr.Add(row);
            //}
            //int n = arr.Count;
            //if (n == 0) { hanghoa = null; }
            //hanghoa = new Entities.HienThiHangHoaListview[n];
            //for (int i = 0; i < n; i++)
            //{
            //    hanghoa[i] = (Entities.HienThiHangHoaListview)arr[i];
            //}
            //lvHienThi.ItemsSource = hanghoa;
            return null;
        }

        /// <summary>
        /// loivt( select hàng hóa và gói hàng trong CSGHH)
        /// </summary>
        public void HienThiCSGHH()
        {
            //Entities.ChinhSachGiaHangHoa don = Common.StaticVariable.InterfaceObject.SelectHangHoaCSGHH();
            Entities.GoiHang gh = new Entities.GoiHang();
            gh.HanhDong = "1";
            Entities.ChinhSachGiaHangHoa don = Common.StaticVariable.InterfaceObject.SelectHangHoaCSGHH(gh);

            int count = don.GoiHang.Length + don.HangHoa.Length;
            Entities.HienThiHangHoaListview[] hanghoa = new Entities.HienThiHangHoaListview[count];
            ArrayList arr = new ArrayList();
            Entities.HienThiHangHoaListview row;
            for (int i = 0; i < don.HangHoa.Length; i++)
            {
                row = new Entities.HienThiHangHoaListview();
                row.MaHangHoa = don.HangHoa[i].MaHangHoa;
                row.TenHangHoa = don.HangHoa[i].TenHangHoa;
                row.ChietKhauThuongMai = don.HangHoa[i].Phantrankhuyenmai;
                row.GiaNhap = don.HangHoa[i].GiaNhap;
                row.Thue = don.HangHoa[i].GiaTriThue;
                arr.Add(row);
            }
            for (int i = 0; i < don.GoiHang.Length; i++)
            {
                row = new Entities.HienThiHangHoaListview();
                row.MaHangHoa = don.GoiHang[i].MaGoiHang;
                row.TenHangHoa = don.GoiHang[i].TenGoiHang;
                row.GiaBanBuon = don.GoiHang[i].GiaBanBuon;
                arr.Add(row);
            }
            int n = arr.Count;
            if (n == 0) { hanghoa = null; }
            hanghoa = new Entities.HienThiHangHoaListview[n];
            for (int i = 0; i < n; i++)
            {
                hanghoa[i] = (Entities.HienThiHangHoaListview)arr[i];
            }
            dgvHienThi.DataSource = hanghoa;
            hienthi2 = hanghoa;
            fix();
        }
        #endregion

        #region Event
        private void frmHangHoa_Load(object sender, EventArgs e)
        {
            this.FormClosing += new FormClosingEventHandler(frmHangHoa_FormClosing);
            btnTim.Click += new EventHandler(btnTim_Click);
            txtTimKiem.TextChanged += new EventHandler(txtTimKiem_TextChanged);
        }

        void dgvHienThi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
                Chon();
        }

        void dgvHienThi_CellDoubleClick2(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
            Chon3();

        }

        void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
            Chon3();
        }

        void btnThoat_Click(object sender, EventArgs e)
        {
            selectedItem = null;
            selectedItemHH = null;
            ask = true;
            this.Close();
        }

        void btnChon_Click(object sender, EventArgs e)
        {
            Chon3();
        }

        void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            TimKiem();
        }

        void btnTim_Click(object sender, EventArgs e)
        {
            TimKiem();
        }

        void frmHangHoa_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (ask)
            {
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    e.Cancel = false;
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
                e.Cancel = false;
        }
        #endregion
    }
}
